// Data processing code for coastal distances, which were computed in ArcGIS (see README.txt for more information).
//
// Jeff Shrader & Matt Gibson
// Creation date: 2014-09-24
// Time-stamp: "2014-02-24 10:25:21 mg"

clear

local work "/DIRECTORY"

capture log close
log using "`work'/logs/Coastal_proc.log", replace
set more off
set matsize 800
timer clear 1
timer on 1

*********************
* Counties
*********************

* Reading county file
* Name is misleading; file includes AK and HI
insheet using "`work'/data/gis/Stata inputs/CntyCoastDistancesLower48.txt", comma clear

* Variable handling
tostring statefp10 countyfp10, replace
gen stlen = length(statefp10)
replace statefp10 = "0" + statefp10 if stlen==1
drop stlen
gen cntylen = length(countyfp10)
replace countyfp10 = "00" + countyfp10 if cntylen==1
replace countyfp10 = "0" + countyfp10 if cntylen==2
drop cntylen
gen fips = statefp10 + countyfp10
rename near_dist coast_dist
label variable coast_dist "Coastal distance"
keep fips coast_dist

* Saving
save "`work'/data/gis/Stata inputs/CntyCoastDistances.dta", replace


*********************
* States
*********************
* Reading state file
insheet using "`work'/data/gis/Stata inputs/StateCoastDistancesLower48.txt", comma clear

* Variable handling
tostring statefp10, replace
gen stlen = length(statefp10)
replace statefp10 = "0" + statefp10 if stlen==1
rename statefp10 state_fips
rename near_dist coast_dist
keep state_fips coast_dist

* Hard-coding dummies for states with coastline
gen st_pacific=0
gen st_atlantic=0
gen st_gulf=0
gen st_greatlakes=0
replace st_pacific = 1 if inlist(state_fips, "06", "41", "53")
replace st_atlantic = 1 if inlist(state_fips, "23", "33", "25", "44", "09", "36", "34") | inlist(state_fips, "10", "24", "51", "37", "45", "13", "12")
replace st_gulf = 1 if inlist(state_fips, "12", "01", "28", "22", "48")
replace st_greatlakes = 1 if inlist(state_fips, "27", "55", "26", "17", "18", "39", "42", "36")

* Saving
save "`work'/data/gis/Stata inputs/StateCoastDistances.dta", replace


*********************
* CBSAs and NECTAs
*********************
* Reading file from ARC
insheet using "`work'/data/gis/Stata inputs/NECTACoastDistances.txt", comma clear

* Variable handling
keep nectafp near_dist
rename nectafp cbsa
tostring cbsa, replace
rename near_dist coast_dist
label variable coast_dist "Coastal distance"

* Saving
save "`work'/data/gis/Stata inputs/NECTACoastDistances.dta", replace

* Reading file from ARC
insheet using "`work'/data/gis/Stata inputs/CBSACoastDistances.txt", comma clear

* Variable handling
keep cbsafp near_dist
rename cbsafp cbsa
tostring cbsa, replace
rename near_dist coast_dist
label variable coast_dist "Coastal distance"

* Saving
save "`work'/data/gis/Stata inputs/CBSACoastDistances.dta", replace


timer off 1
timer list 1
capture log close


